/**
 * 
 */
package com.rainripple.java.common.dao;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.mybatis.spring.SqlSessionTemplate;

import com.rainripple.java.common.exception.DaoException;

/**
 * @author lsfe
 * 
 */
@Singleton
@Named
public class Dao implements IDao {


	@Inject
	private SqlSessionTemplate sqlSession;

	public void setSqlSession(SqlSessionTemplate sqlSession) {
		this.sqlSession = sqlSession;
	}

	private String getMapper(Class<?> entityClass, String id) {
		return entityClass.getName() + "Mapper."+id;
	}

	public static final String updateByPrimaryKeySelective = "updateByPrimaryKeySelective";
	public static final String updateByPrimaryKey = "updateByPrimaryKey";
	public static final String deleteByPrimaryKey = "deleteByPrimaryKey";
	public static final String deleteSelective = "deleteSelective";
	public static final String selectSelectiveCount = "selectSelectiveCount";
	public static final String selectSelective = "selectSelective";
	public static final String selectByPrimaryKey = "selectByPrimaryKey";
	public static final String insert = "insert";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#save(java.lang.Object)
	 */
	@Override
	public void save(Object entity) {
		if (null == entity) {
			throw new DaoException();
		}
		insert(getMapper(entity.getClass(), insert), entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#save(java.lang.Class,
	 * java.lang.Object)
	 */
	@Override
	public void save(Class<?> cls, Object entity) {
		insert(getMapper(entity.getClass(), insert), entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#update(java.lang.Object)
	 */
	@Override
	public int update(Object entity) {
		if (null == entity) {
			throw new DaoException();
		}
		return update(getMapper(entity.getClass(), updateByPrimaryKey), entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#update(java.lang.Class,
	 * java.lang.Object)
	 */
	@Override
	public int update(Class<?> cls, Object parameter) {
		if (null == parameter) {
			throw new DaoException();
		}
		return update(getMapper(cls, updateByPrimaryKeySelective), parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#delete(java.lang.Object)
	 */
	@Override
	public void delete(Object entity) {
		if (null == entity) {
			throw new DaoException();
		}
		delete(getMapper(entity.getClass(), deleteByPrimaryKey), entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#delete(java.lang.Class,
	 * java.lang.Object)
	 */
	@Override
	public void delete(Class<?> entityClass, Object parameter) {
		delete(getMapper(entityClass, deleteSelective), parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#get(java.lang.Class,
	 * java.lang.Object)
	 */
	@Override
	public <T> T get(Class<T> entityClass, Object id) {
		List<T> list = selectList(getMapper(entityClass, selectByPrimaryKey), id);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.Class)
	 */
	@Override
	public <T> List<T> selectList(Class<T> entityClass) {
		return selectList(getMapper(entityClass,selectSelective),null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.Class, int,
	 * int)
	 */
	@Override
	public <T> List<T> selectList(Class<T> entityClass, int limit, int start) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.Class,
	 * java.lang.Object)
	 */
	@Override
	public <T> List<T> selectList(Class<T> entityClass, T prarm) {
		// TODO Auto-generated method stub
		return selectList(getMapper(entityClass,selectSelective),prarm);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.Class,
	 * java.lang.Object, int, int)
	 */
	@Override
	public <T> List<T> selectList(Class<T> entityClass, T prarm, int limit, int start) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#insert(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public int insert(String statement, Object parameter) {
		return sqlSession.insert(statement, parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#update(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public int update(String statement, Object parameter) {
		return sqlSession.update(statement, parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#delete(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public int delete(String statement, Object parameter) {
		return sqlSession.delete(statement, parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectOne(java.lang.String)
	 */
	@Override
	public <T> T selectOne(String statement) {
		return sqlSession.selectOne(statement);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectOne(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public <T> T selectOne(String statement, Object parameter) {
		// TODO Auto-generated method stub
		return sqlSession.selectOne(statement, parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.String)
	 */
	@Override
	public <T> List<T> selectList(String statement) {
		return sqlSession.selectList(statement);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public <T> List<T> selectList(String statement, Object parameter) {
		return sqlSession.selectList(statement, parameter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rainripple.java.common.dao.IDao#selectList(java.lang.String,
	 * java.lang.Object, int, int)
	 */
	@Override
	public <E> List<E> selectList(String statement, Object parameter, int limit, int start) {
		// TODO Auto-generated method stub
		return null;
	}

}
